package com.ruoyi.ftrl.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ftrl.domain.AssociationInfo;
import com.ruoyi.ftrl.domain.RoomInfo;
import com.ruoyi.ftrl.mapper.AssociationInfoMapper;
import com.ruoyi.ftrl.mapper.BuildingInfoMapper;
import com.ruoyi.ftrl.mapper.RoomInfoMapper;
import com.ruoyi.ftrl.service.IRoomInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
 * 房间信息Service业务层处理
 *
 * @author dihang
 * @date 2024-07-08
 */
@Service
public class RoomInfoServiceImpl implements IRoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private BuildingInfoMapper buildingInfoMapper;

    /**
     * 查询房间信息
     *
     * @param id 房间信息主键
     * @return 房间信息
     */
    @Override
    public RoomInfo selectRoomInfoById(Long id) {
        return roomInfoMapper.selectRoomInfoById(id);
    }

    /**
     * 查询房间信息列表
     *
     * @param roomInfo 房间信息
     * @return 房间信息
     */
    @Override
    public List<RoomInfo> selectRoomInfoList(RoomInfo roomInfo) {
        return roomInfoMapper.selectRoomInfoList(roomInfo);
    }

    /**
     * 新增房间信息
     *
     * @param roomInfo 房间信息
     * @return 结果
     */
    @Override
    public int insertRoomInfo(RoomInfo roomInfo) {
        roomInfo.setCreateTime(DateUtils.getNowDate());
        roomInfo.setCreateBy(SecurityUtils.getUsername());
        //添加所属楼栋时将该楼栋的id进行存储，形成二级目录
        roomInfo.setBuildingId(buildingInfoMapper.selectBuildingInfoByName(roomInfo.getBuildingName()).getId());
        RoomInfo roomInfo1 = roomInfoMapper.selectRoomInfoByName(roomInfo.getHouseNumber());
        //生成唯一房间编号
        String roomNo = generateUniqueRoomNumber();
        roomInfo.setRoomNo(roomNo);
        return roomInfoMapper.insertRoomInfo(roomInfo);
    }

    /**
     *  生成唯一房间编号
     * @return
     */
    private static Set<String> generatedNumbers = new HashSet<>();

    public static String generateUniqueRoomNumber() {
        String prefix = "41260";
        Random random = new Random();
        String roomNumber;
        do {
            int suffix = random.nextInt(100000);
            String formattedSuffix = String.format("%05d", suffix);
            roomNumber = prefix + formattedSuffix;
        } while (generatedNumbers.contains(roomNumber));
        generatedNumbers.add(roomNumber);
        return roomNumber;
    }

    /**
     * 修改房间信息
     *
     * @param roomInfo 房间信息
     * @return 结果
     */
    @Override
    public int updateRoomInfo(RoomInfo roomInfo) {
        roomInfo.setUpdateTime(DateUtils.getNowDate());
        roomInfo.setUpdateBy(SecurityUtils.getUsername());
        return roomInfoMapper.updateRoomInfo(roomInfo);
    }

    /**
     * 批量删除房间信息
     *
     * @param ids 需要删除的房间信息主键
     * @return 结果
     */
    @Override
    public int deleteRoomInfoByIds(Long[] ids) {
        return roomInfoMapper.deleteRoomInfoByIds(ids);
    }

    /**
     * 删除房间信息信息
     *
     * @param id 房间信息主键
     * @return 结果
     */
    @Override
    public int deleteRoomInfoById(Long id) {
        return roomInfoMapper.deleteRoomInfoById(id);
    }

    /**
     * 根据楼栋id获取房间集合
     *
     * @param id
     * @return
     */
    @Override
    public List<RoomInfo> selectRoomInfoByParentId(Long id) {
        return roomInfoMapper.selectRoomInfoByParentId(id);
    }

    @Autowired
    private AssociationInfoMapper associationInfoMapper;
    /**
     * 识别房间户型（根据小区名称）
     * @param name
     * @return
     */
    @Override
    public int updateRoomInfoState(String name) {
        //查询出该小区的所有房间
        List<RoomInfo> roomInfoList = roomInfoMapper.selectRoomInfoByNames(name);
        //根该房间的上、下房间的供暖状态来判断该该房间的最新户型状态
        for (RoomInfo roomInfo : roomInfoList) {
            //房间编号
            String roomNo = roomInfo.getRoomNo();
            //楼栋
            String buildingName = roomInfo.getBuildingName();
            //单元
            String unitName = roomInfo.getUnitName();
            //房间号
            String houseNumber = roomInfo.getHouseNumber();
            //获取上层房间的供暖状态 供暖|其他
            //上层房间号
            int upperRoom = Integer.parseInt(houseNumber) + 100;
            String string = String.valueOf(upperRoom);
            //获取下层房间的供暖状态 供暖|其他
            //查询下层房间号
            //下层房间号
            int lowerRoom = Integer.parseInt(houseNumber) - 100;
            String string1 = String.valueOf(lowerRoom);
            //查询该房间的供暖状况
            AssociationInfo associationInfo = new AssociationInfo();
            associationInfo.setBuildingName(buildingName);
            associationInfo.setUnitName(unitName);
            associationInfo.setHouseNumber(houseNumber);
            associationInfo.setCommunityName(name);
            List<AssociationInfo> infoList = associationInfoMapper.selectAssociationInfoList(associationInfo);
            if (infoList == null || infoList.isEmpty()) {
                System.err.println("未检测到房间的供暖信息" + buildingName + "-" + unitName + "-" + houseNumber);
            } else {//有相对应的房间时
                //本房间的供暖状况
                AssociationInfo associationInfo1 = infoList.get(0);
                String heatState = associationInfo1.getHeatState();
                if (heatState.isEmpty()) {
                    System.err.println("未检测到房间的供暖信息" + buildingName + "-" + unitName + "-" + houseNumber);
                } else if (!heatState.equals("供热")) {
                    //为报停户
                    roomInfoMapper.updateRoomInfoByRoomNoFour(roomNo);
                } else {
                    if (houseNumber.charAt(0) == '1') {//这间房间是底层
                        //查询上层房间号
                        AssociationInfo roomInfo1 = new AssociationInfo();
                        roomInfo1.setCommunityName(name);
                        roomInfo1.setBuildingName(buildingName) ;
                        roomInfo1.setUnitName(unitName);
                        roomInfo1.setHouseNumber(string);
                        associationInfo.setCommunityName(name);
                        //上层房间详情信息
                        List<AssociationInfo> roomInfos = associationInfoMapper.selectAssociationInfoList(roomInfo1);
                        if (roomInfos == null || roomInfos.isEmpty()) {
                            System.err.println("未查询到上层房间" + buildingName + "-" + unitName + "-" + string + "信息，无法判断户型！");
                        } else {
                            AssociationInfo roomInfo2 = roomInfos.get(0);
                            //上层房间供暖状态
                            String upHeatState = roomInfo2.getHeatState();
                            //先判断自身房间是否为报停户
                            if (upHeatState.equals("供热")) {//为下停户
                                //直接更新房间表中的数据
                                roomInfoMapper.updateRoomInfoByRoomNo(roomNo);
                            } else {//为孤岛户
                                //直接更新房间表中的数据
                                roomInfoMapper.updateRoomInfoByRoomNoZero(roomNo);
                            }
                        }

                        //对于小区楼栋的顶层的不统一的情况
                    } else if ((buildingName.equals("3") || buildingName.equals("4") || buildingName.equals("5")
//                            || buildingName.equals("17")||buildingName.equals("21")
//                            ||buildingName.equals("6")||buildingName.equals("12")||buildingName.equals("13")
                    )
                            && houseNumber.substring(0, 2).equals("18")) {//这间房间是顶层
                        AssociationInfo roomInfo3 = new AssociationInfo();
                        roomInfo3.setCommunityName(name);
                        roomInfo3.setBuildingName(buildingName);
                        roomInfo3.setUnitName(unitName);
                        roomInfo3.setHouseNumber(string1);
                        associationInfo.setCommunityName(name);
                        List<AssociationInfo> associationInfos = associationInfoMapper.selectAssociationInfoList(roomInfo3);
                        if (associationInfos == null || associationInfos.isEmpty()) {
                            System.err.println("未查询到下层房间" + buildingName + "-" + unitName + "-" + string1 + "信息，无法判断户型！");
                        } else {
                            //下层房间详情信息
                            AssociationInfo roomInfo4 = associationInfos.get(0);
                            //根据上下房间的供暖状况判断当前房间的户型
                            //下层房间供暖状态
                            String upHeatState1 = roomInfo4.getHeatState();

                            if (upHeatState1.equals("供热")) {//为上停户
                                //直接更新房间表中的数据
                                roomInfoMapper.updateRoomInfoByRoomNoOne(roomNo);
                            } else {//为孤岛户
                                //直接更新房间表中的数据
                                roomInfoMapper.updateRoomInfoByRoomNoZero(roomNo);
                            }
                        }

                    } else if ((buildingName.equals("6")
//                            || buildingName.equals("6")
//                            || buildingName.equals("7")|| buildingName.equals("8")|| buildingName.equals("9")
//                            || buildingName.equals("14")|| buildingName.equals("15")
                            ) && houseNumber.substring(0, 2).equals("19")) {//这间房间是顶层
                        AssociationInfo roomInfo3 = new AssociationInfo();
                        roomInfo3.setCommunityName(name);
                        roomInfo3.setBuildingName(buildingName);
                        roomInfo3.setUnitName(unitName);
                        roomInfo3.setHouseNumber(string1);
                        associationInfo.setCommunityName(name);
                        List<AssociationInfo> associationInfos = associationInfoMapper.selectAssociationInfoList(roomInfo3);
                        if (associationInfos == null || associationInfos.isEmpty()) {
                            System.err.println("未查询到下层房间" + buildingName + "-" + unitName + "-" + string1 + "信息，无法判断户型！");
                        } else {
                            //下层房间详情信息
                            AssociationInfo roomInfo4 = associationInfos.get(0);
                            //根据上下房间的供暖状况判断当前房间的户型
                            //下层房间供暖状态
                            String upHeatState1 = roomInfo4.getHeatState();

                            if (upHeatState1.equals("供热")) {//为上停户
                                //直接更新房间表中的数据
                                roomInfoMapper.updateRoomInfoByRoomNoOne(roomNo);
                            } else {//为孤岛户
                                //直接更新房间表中的数据
                                roomInfoMapper.updateRoomInfoByRoomNoZero(roomNo);
                            }
                        }


                    } else if ((buildingName.equals("7")
//                            || buildingName.equals("2")|| buildingName.equals("33")
                    ) &&houseNumber.substring(0, 2).equals("22")) {
//                    ) && houseNumber.charAt(0) == '5') {
                        AssociationInfo roomInfo3 = new AssociationInfo();
                        roomInfo3.setCommunityName(name);
                        roomInfo3.setBuildingName(buildingName);
                        roomInfo3.setUnitName(unitName);
                        roomInfo3.setHouseNumber(string1);
                        associationInfo.setCommunityName(name);
                        List<AssociationInfo> associationInfos = associationInfoMapper.selectAssociationInfoList(roomInfo3);
                        if (associationInfos == null || associationInfos.isEmpty()) {
                            System.err.println("未查询到下层房间" + buildingName + "-" + unitName + "-" + string1 + "信息，无法判断户型！");
                        } else {
                            //下层房间详情信息
                            AssociationInfo roomInfo4 = associationInfos.get(0);
                            //根据上下房间的供暖状况判断当前房间的户型
                            //下层房间供暖状态
                            String upHeatState1 = roomInfo4.getHeatState();

                            if (upHeatState1.equals("供热")) {//为上停户
                                //直接更新房间表中的数据
                                roomInfoMapper.updateRoomInfoByRoomNoOne(roomNo);
                            } else {//为孤岛户
                                //直接更新房间表中的数据
                                roomInfoMapper.updateRoomInfoByRoomNoZero(roomNo);
                            }
                        }

                    } else if ((buildingName.equals("1") || buildingName.equals("2")
//                            || buildingName.equals("34")
                    ) && houseNumber.substring(0, 2).equals("27")) {//这间房间是顶层
//                    ) && houseNumber.charAt(0) == '6') {//这间房间是顶层
                        AssociationInfo roomInfo3 = new AssociationInfo();
                        roomInfo3.setCommunityName(name);
                        roomInfo3.setBuildingName(buildingName);
                        roomInfo3.setUnitName(unitName);
                        roomInfo3.setHouseNumber(string1);
                        associationInfo.setCommunityName(name);
                        List<AssociationInfo> associationInfos = associationInfoMapper.selectAssociationInfoList(roomInfo3);
                        if (associationInfos == null || associationInfos.isEmpty()) {
                            System.err.println("未查询到下层房间" + buildingName + "-" + unitName + "-" + string1 + "信息，无法判断户型！");
                        } else {
                            //下层房间详情信息
                            AssociationInfo roomInfo4 = associationInfos.get(0);
                            //根据上下房间的供暖状况判断当前房间的户型
                            //下层房间供暖状态
                            String upHeatState1 = roomInfo4.getHeatState();

                            if (upHeatState1.equals("供热")) {//为上停户
                                //直接更新房间表中的数据
                                roomInfoMapper.updateRoomInfoByRoomNoOne(roomNo);
                            } else {//为孤岛户
                                //直接更新房间表中的数据
                                roomInfoMapper.updateRoomInfoByRoomNoZero(roomNo);
                            }
                        }

                    }
                    //中间楼层的户型判断
                    else {
                        //查询上层房间号
                        AssociationInfo roomInfo1 = new AssociationInfo();
                        roomInfo1.setCommunityName(name);
                        roomInfo1.setBuildingName(buildingName);
                        roomInfo1.setUnitName(unitName);
                        roomInfo1.setHouseNumber(string);
                        List<AssociationInfo> associationInfos = associationInfoMapper.selectAssociationInfoList(roomInfo1);
                        if (associationInfos == null || associationInfos.isEmpty()) {
                            System.err.println("未查询到上层房间" + buildingName + "-" + unitName + "-" + string + "信息，无法判断户型！");
                        } else {
                            //上层房间详情信息
                            AssociationInfo roomInfo2 = associationInfos.get(0);
                            //上层房间供暖状态
                            String upHeatState = roomInfo2.getHeatState();

                            AssociationInfo roomInfo3 = new AssociationInfo();
                            roomInfo3.setCommunityName(name);
                            roomInfo3.setBuildingName(buildingName);
                            roomInfo3.setUnitName(unitName);
                            roomInfo3.setHouseNumber(string1);
                            List<AssociationInfo> associationInfos1 = associationInfoMapper.selectAssociationInfoList(roomInfo3);
                            if (associationInfos1 == null || associationInfos1.isEmpty()) {
                                System.err.println("未查询到下层房间" + buildingName + "-" + unitName + "-" + string1 + "信息，无法判断户型！");
                            } else {
                                //下层房间详情信息
                                AssociationInfo roomInfo4 = associationInfoMapper.selectAssociationInfoList(roomInfo3).get(0);
                                //根据上下房间的供暖状况判断当前房间的户型
                                //下层房间供暖状态
                                String upHeatState1 = roomInfo4.getHeatState();

                                if (upHeatState.equals("供热") && upHeatState1.equals("供热")) {//中间户
                                    roomInfoMapper.updateRoomInfoByRoomNoThree(roomNo);
                                } else if (upHeatState.equals("供热")) {//下停户
                                    roomInfoMapper.updateRoomInfoByRoomNo(roomNo);
                                } else if (upHeatState1.equals("供热")) {//上停户
                                    roomInfoMapper.updateRoomInfoByRoomNoOne(roomNo);
                                } else {//孤岛户
                                    roomInfoMapper.updateRoomInfoByRoomNoZero(roomNo);
                                }
                            }

                        }

                    }
                }
            }
        }
        return 1;
    }
            }













